from lxml import etree
import requests
from fake_useragent import *


# xpath的使用

def demo01():
    url = "https://www.qidian.com/rank/fengyun?style=1"
    headers = {
        "User-agent": UserAgent().chrome
    }
    response = requests.get(url, headers=headers)
    html = etree.HTML(response.text)
    names = html.xpath("//h4/a/text()")
    authors = html.xpath("//p[@class='author']/a[1]/text()")
    # print(names)
    # print(authors)
    # for num in range(len(names)):
    #     print(f"{names[num]} : {authors[num]}")

    for name, author in zip(names, authors):
        print(f"{name} : {author}")


def demo02():
    url = "https://www.kuaidaili.com/free/"
    headers = {
        "User-agent": UserAgent().chrome
    }
    response = requests.get(url, headers=headers)
    html = etree.HTML(response.text)
    tds = html.xpath("//tbody/tr/td/text()")
    trs_len = len(html.xpath("//tbody/tr"))
    for num in range(trs_len):
        ip = tds[num * 7]
        port = tds[num * 7 + 1]
        type = tds[num * 7 + 3]
        print(f"{ip}:{port}   {type}")


if __name__ == '__main__':
    demo01()
    demo02()
